-- database access

db = {}

db.user = "root"
db.password=nil
db.database="search"
db.provider = luasql.mysql

local err
db.conn, err = db.provider():connect(db.database, db.user, db.password)

if err then
	print(err)
	os.exit()

end

db.execute = function(q, params)
	if params then
		q, _ = q:gsub("@(%w+)@", function(param) return params[param] end)
	--	debug_t(params)
	
	end
	print("q:"..q)
		
	return db.conn:execute(q)
end

--[[ 
execute the file named 'fname'
Optionally, you can do paramater substituting by surrounding paramater names with '@@' eg:

q.sql:

SELECT * FROM Thing WHERE field = '@@q@@'

q.lua:
db.execute_file('q.lua', {q="Search for this"})
result:
SELECT * FROM Thing WHERE field = 'Search for this'


]]
db.execute_file = function(fname, params)
	for _, text in ipairs(string.split(io.open(fname, "r"):read("*a"), ";")) do
		print(db.execute(text, params))
	end
end


